home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Graphics / sKulpt / skulpt-src / Ami-Texture.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-30  |  4.0 KB  |  205 lines

  1. /*
  2. #include <exec/types.h>
  3. #include <exec/memory.h>
  4. #include <libraries/asl.h>
  5. #include <proto/exec.h>
  6. #include <proto/intuition.h>
  7. #include <proto/dos.h>
  8. #include <proto/graphics.h>
  9. #include <proto/Warp3D.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <math.h>
  14.  
  15. #include <cybergraphx/cybergraphics.h>
  16. #include <clib/cybergraphics_protos.h>
  17. #include <pragma/cybergraphics_lib.h>
  18. */
  19. #define STRICT
  20.  
  21. // Includes standard Windows
  22. #include <windows.h>
  23. #include <windowsx.h>
  24. #include <time.h>
  25. #include <stdlib.h>
  26. #include <malloc.h>
  27. #include <memory.h>
  28. #include <stdio.h>
  29.  
  30. // Includes D3D
  31. #define  D3D_OVERLOADS
  32. #include <ddraw.h>
  33. #include <d3d.h>
  34.  
  35. // Includes utilitaires D3D
  36. #include "d3dmath.h"
  37. #include "d3dutil.h"
  38. #include "D3DEnum.h"
  39.  
  40. #include <d3dx.h>
  41.  
  42. // Ids Resources
  43. #include "resource.h"
  44.  
  45. // Constantes
  46. #include "const.h"
  47.  
  48. // Types
  49. #include "types.h"
  50.  
  51. // Variables globales projet
  52. #include "vars.h"
  53.  
  54. // Prototypes fonctions autres modules
  55. #include "proto.h"
  56.  
  57. // Macros
  58. #include "macros.h"
  59.  
  60. W3D_Texture *tex = NULL;
  61. void *texmap = NULL;
  62.  
  63. UBYTE *LoadTextureFromPPM(APTR where, char *filename, UBYTE Opacity, int *w, int *h)
  64. {
  65.     ULONG *map, *map2;
  66.     FILE *f;
  67.     int i,j;
  68.     UBYTE r,g,b;
  69.     unsigned long x,y;
  70.     int opqy, opqx, o;
  71.  
  72.     f=fopen(filename, "r");
  73.  
  74.     if (!f) {
  75.         vTrace("Error: Unable to open file '%s'", filename);
  76.         return NULL;
  77.     }
  78.  
  79. #ifndef __STORM__
  80.     i=fscanf(f, "P6\n%ld %ld\n255\n", &x, &y);
  81. #else
  82.     i=fscanf(f, "P6\n%ld\n%ld\n255\n", &x, &y);
  83. #endif
  84.  
  85.     if (i!=2) {
  86.         vTrace("Error: This seems to be no PPM file");
  87.         fclose(f);
  88.         return NULL;
  89.     }
  90.  
  91.     *w = x;
  92.     *h = y;
  93.  
  94.     if (where==NULL) {
  95.         map2 = map = (ULONG *) AllocVec(x*y*4, MEMF_PUBLIC);
  96.     } else {
  97.         map = map2 = (ULONG *) where;
  98.     }
  99.     if (!map) {
  100.         fclose(f);
  101.         vTrace("Error: Out of memory");
  102.         return NULL;
  103.     }
  104.  
  105.     opqy = 256/y;
  106.     opqx = 256/x;
  107.     opqx = opqx>>1;
  108.     o=0;
  109.     for (i=0; i<y; i++) {
  110.         for(j=0; j<x; j++) {
  111.             if (o+j*opqy > 255)
  112.                 Opacity = 255;
  113.             else
  114.                 Opacity = (UBYTE)(o+j*opqy);
  115.  
  116.             r=(UBYTE)fgetc(f);
  117.             g=(UBYTE)fgetc(f);
  118.             b=(UBYTE)fgetc(f);
  119.             *map = (Opacity<<24) | ((r&0xFF)<<16) | ((g&0xFF)<<8) | (b&0xFF);
  120.             map++;
  121.         }
  122.         o += opqx;
  123.     }
  124.  
  125.     fclose(f);
  126.     return (UBYTE *)map2;
  127. }
  128.  
  129. BOOL GenTexture(W3D_Context* context, char* name)
  130. {
  131.     UBYTE *where;
  132.     int w, h;
  133.     ULONG error;
  134.  
  135.     // This loads a ppm (24 bit) texture file from disk, and sets
  136.     // it to 50% transparency (0x7F)...
  137.     where = LoadTextureFromPPM(NULL, name, 0x7F, &w, &h);
  138.     if (!where) return FALSE;
  139.     vTrace("Size: %ld×%ld", w,h);
  140.  
  141.     texmap = where;
  142.  
  143.     tex = W3D_AllocTexObjTags(context, &error,
  144.         W3D_ATO_IMAGE,      (ULONG)where,   // The image data
  145.         W3D_ATO_FORMAT,     W3D_A8R8G8B8,   // This is a 32 bit image
  146.         W3D_ATO_WIDTH,      w,              // Length of one side
  147.         W3D_ATO_HEIGHT,     h,
  148.     TAG_DONE);
  149.  
  150.     vTrace("Texture created");
  151.  
  152.     if (!tex || error != W3D_SUCCESS) {
  153.         vTrace("Error generating texture: ");
  154.         switch(error) {
  155.         case W3D_ILLEGALINPUT:
  156.             vTrace("Illegal input");
  157.             break;
  158.         case W3D_NOMEMORY:
  159.             vTrace("Out of memory");
  160.             break;
  161.         case W3D_UNSUPPORTEDTEXSIZE:
  162.             vTrace("Unsupported texture size");
  163.             break;
  164.         case W3D_NOPALETTE:
  165.             vTrace("Chunky texture without palette specified");
  166.             break;
  167.         case W3D_UNSUPPORTEDTEXFMT:
  168.             vTrace("Texture format not supported");
  169.             break;
  170.         default:
  171.             vTrace("ahem... An error has occured");
  172.         }
  173.         return FALSE;
  174.     }
  175.  
  176. //      W3D_UploadTexture(context, tex); // AutoTexManagement will do this for me !-)
  177.  
  178.     return TRUE;
  179. }
  180.  
  181. void vInitTextures(W3D_Context *context)
  182. {
  183.     if (FALSE == GenTexture(context, "wall.ppm")) {
  184.         vTrace("Error loading/generating default texture");
  185.         return;
  186.     }
  187.  
  188.     // Set texture wrapping mode to on.
  189.     W3D_SetWrapMode(context, tex, W3D_REPEAT, W3D_REPEAT, NULL);
  190.  
  191.     // Set bilinear filter
  192.     W3D_SetFilter(context, tex, W3D_LINEAR, W3D_LINEAR);
  193.  
  194.     // Set perspective correction
  195. //    W3D_SetState(context, W3D_PERSPECTIVE, W3D_ENABLE);
  196. }
  197.  
  198. void vCloseTextures(W3D_Context *context)
  199. {
  200.     W3D_FlushTextures(context);
  201.     if (tex) { W3D_FreeTexObj(context, tex); tex = NULL; }
  202.     if (texmap) {       FreeVec(texmap); texmap = NULL; }
  203. }
  204.  
  205.